package com.ta.gulimall.auth.controller;

import cn.hutool.core.util.StrUtil;
import com.ta.gulimall.common.utils.system.R;
import com.ta.gulimall.auth.service.RegisterService;
import com.ta.gulimall.auth.view.vo.UserRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class RegisterController {
    @Autowired
    RegisterService registerService;

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @GetMapping("/sms/sendCode")
    @ResponseBody
    public R sendCode(String phone){
        if (StrUtil.isBlank(phone)) {
            throw new RuntimeException("手机号不能为空!");
        }
        registerService.sendCode(phone);
        return R.ok();
    }

    /**
     * 注册账号
     * @param vo
     * @param session
     * @return
     */
    @PostMapping("/register")
    public String register(@Validated UserRegisterVo vo, HttpSession session){
        registerService.register(vo);

        return "redirect:http://gulimall.com/login.html";
    }
}
